﻿namespace EFS.RC.CCAdmin.Surveys.Survey
{
	/// Class List
	public partial class List : EFS.RC.CCAdmin.Common.Control
	{
		#region Constructors
		/// Implicit
		public List()
		{
			InitializeComponent();

			// ignore in designer
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//set listener up for list load
				UtilitiesService.Survey_GetAllCompleted += UtilitiesService_Survey_GetAllCompleted;

				//set delete response listener
				UtilitiesService.Survey_DeleteCompleted += UtilitiesService_Survey_DeleteCompleted;
			}
		}
		#endregion

		#region LoadData
		/// LoadData
		private void LoadData()
		{
			// Load surveys
			LoadSurveys();
		}

		/// LoadSurveys
		private void LoadSurveys()
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while retrieving data");

			//call ws
			UtilitiesService.Survey_GetAllAsync(null, null, null);
		}
		#endregion

		#region WS callbacks
		/// Get all surveys completed
		private void UtilitiesService_Survey_GetAllCompleted(object sender, WebProxy.Survey_GetAllCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try 
			{
				//check if request was successful
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] surveys =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[]>(e.Result.ToString());

				// bind/fill form
				dataGridSurveys.ItemsSource = surveys;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data. " + ex.Message);
			}
		}

		/// Delete survey completed
		private void UtilitiesService_Survey_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			// reset
			dataGridSurveys.ItemsSource = null;

			try
			{
				//check if request was successful
				if (null != e.Error)
				{
					throw e.Error;
				}
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to delete monitoring! " + ex.Message);
			}

			// Load surveys
			LoadSurveys();
		}
		#endregion

		#region CRUD Click listeners

		//edit survey button click in datagrid
		private void EditLink_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo survey = (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo) dataGridSurveys.SelectedItem;

			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Surveys.Survey.Edit(wnd, survey.ID);
			wnd.Show(OnEditWindowClosed);
		}

		//delete button click in datagrid
		private void DeleteLink_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			if (System.Windows.Browser.HtmlPage.Window.Confirm("Permanently delete monitoring?"))
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while deleting monitoring");

				EFS.RC.CC.CH.Entities.Surveys.SurveyInfo survey = (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo)dataGridSurveys.SelectedItem;
				UtilitiesService.Survey_DeleteAsync(survey.ID.Value);
			}
		}

		#endregion

		#region Window callbacks
		//add new survey
		private void OnNewWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			// load surveys
			LoadSurveys();
		}

		//update survey
		private void OnEditWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason != EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok)
			{
				return;
			}

			// load surveys
			LoadSurveys();
		}
		#endregion

		#region Events
		/// Tool bar click
		private void ToolbarItem_New_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			EFS.Common.UI.Controls.Toolbar.ToolbarItem toolbarItem = (EFS.Common.UI.Controls.Toolbar.ToolbarItem)sender;

			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Surveys.Survey.Edit(wnd, null);
			wnd.Show(OnNewWindowClosed);
		}

		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			//load data
			LoadData();
		}
		#endregion
	}

}